package com.jus.player.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.jus.player.R;

import java.util.ArrayList;
import java.util.List;

public class UnlockDialogAdapter extends RecyclerView.Adapter<UnlockDialogAdapter.ViewHolder> {

    private final List<Integer> episodes;
    private final List<Integer> selectedEpisodes = new ArrayList<>();
    //private final List<Integer> tempList = new ArrayList<>();
    private final OnItemCheckBoxClickListener listener;
    private int defaultEpisode;
    private Context context;
    private Boolean allSelected = null;
    public UnlockDialogAdapter(List<Integer> episodes, OnItemCheckBoxClickListener listener, int defaultEpisode) {
        this.episodes = episodes;
        this.listener = listener;
        this.defaultEpisode = defaultEpisode;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_unlock_dialog, parent, false);
        context = view.getContext();
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Integer episode = episodes.get(position);
        holder.textEpisode.setText(context.getString(R.string.item_episode, episode));
        if (allSelected != null) {
            holder.checkBox.setChecked(allSelected);
        } else {
            if (episode.equals(defaultEpisode)) {
                holder.checkBox.setChecked(true);
                defaultEpisode = 0;
                selectedEpisodes.add(episode);
            } else {
                holder.checkBox.setChecked(selectedEpisodes.contains(episode));
            }
        }
        holder.checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                selectedEpisodes.add(episode);
            } else {
                selectedEpisodes.remove(episode);
            }
            listener.onClickUpdateData();
        });
        listener.onClickUpdateData();
    }

    @Override
    public int getItemCount() {
        return episodes.size();
    }

    public List<Integer> getSelectedEpisodes() {
        return selectedEpisodes;
    }

    public void selectAll(boolean all) {
        selectedEpisodes.clear();
        if (all) {
            selectedEpisodes.addAll(episodes);
        }
        allSelected = all;
        notifyDataSetChanged();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView textEpisode;
        CheckBox checkBox;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            textEpisode = itemView.findViewById(R.id.textEpisode);
            checkBox = itemView.findViewById(R.id.checkBox);
        }
    }

    public interface OnItemCheckBoxClickListener {
        void onClickUpdateData();
    }
}
